<template>
  <div class="app-box">
    <Layout ref="runBox"
            title="Weighting Auto 终端"
            temp="Temp2"
            :autoCommit="false"
            @handleSuccessMqttMessage="handleSuccessMqttMessage"
            @readIcCardSuccess="readIcCardSuccess"
            :ic="ic"
            :url="url"
            :readID="false"
            :readIC="true"></Layout>
  </div>
</template> 
<script>
import Layout from '../core/layout.vue'
import { GSAConstants } from '@/utils/access'
import {
  log,
} from '@/utils/message'
import { ipcRenderer } from 'electron'
export default {
  components: { Layout },
  data () {
    return {
      ic: GSAConstants().TOPIC_PREADVICE_WEIGHT_WEIGHT_AUTO.ic,
      url: GSAConstants().TOPIC_PREADVICE_WEIGHT_WEIGHT_AUTO.url,
      readThread: null,
    }
  },
  created () {
  },
  mounted () {
    this.registerWeight()
  },
  methods: {
    showMsg (msg, type) {
      this.$refs.runBox.showMsg(msg, type)
    },
    registerWeight () {
      log('== 开始加载地磅DLL ==')
      ipcRenderer.on('load-weight-dll-thread-reply', (event, res) => {
        log('====load-weight-dll-thread-reply====', res)
      })

      ipcRenderer.on('read-weight-reply', (event, res) => {
        log('====read-weight-reply====', res)

        this.writeWeight(res)
      })


      ipcRenderer.send('load-weight-dll-thread')
    },
    // 读到卡的回掉
    writeWeight (res) {
      // {"code":-1,"msg":"Unable to open com port (Error: 2)"}
      if (res.code === -1) {
        log('地磅称重异常', res.msg)
      }

      // read success 
      if (res.code === 1) {
        this.clearReadWeight()

        const qd = {
          weight: res.weight
        }
        this.$refs.runBox.setForm(qd)

        // 刷新页面
        this.$refs.runBox.refreshPage()

        // 提交数据
        this.$refs.runBox.doPageFun('submit')
      }
    },
    getWeightMode () {
      const terminalDtl = this.$store.getters.app.terminalDtl
      if (terminalDtl) {
        return terminalDtl.isWeightBridge
      }

      return ''
    },
    getMeterType () {
      const terminalDtl = this.$store.getters.app.terminalDtl
      if (terminalDtl) {
        return terminalDtl.wbMeterType
      }

      return ''
    },
    // 停止读卡线程
    clearReadWeight () {
      if (this.readThread) {
        clearInterval(this.readThread)
      }
    },
    // 称重
    readWeight () {
      // if (true) {
      //   this.writeWeight({ code: 1, weight: 10 })
      //   return
      // }
      this.clearReadWeight()
      this.readThread = setInterval(() => {

        ipcRenderer.send('read-weight', this.parseMeterType(this.getMeterType()))
      }, 1500)
    },
    // 系统配置的MeterType 跟 dll包中的类型做适配
    parseMeterType (meterType) {
      if (meterType === 'DATANG') {
        return 1
      }
      if (meterType === 'NEWDATANG') {
        return 2
      }
      if (meterType === 'CHIMEITX') {
        return 3
      }
      if (meterType === 'TOLEDO') {
        return 4
      }

      log('未知的地磅终端类型', meterType)
      return 0
    },
    // 读IC卡成功， 开始称重
    readIcCardSuccess () {
      const weightMode = this.getWeightMode()
      if (weightMode !== 'Y') {
        // 提交数据
        this.$refs.runBox.doPageFun('submit')
        return;
      }

      // 开始称重
      // ipcRenderer.send('read-weight', this.parseMeterType(this.getMeterType()))
      this.readWeight()
    },
    // 读卡成功的回调信息
    handleSuccessMqttMessage (messageObject) {
      if (messageObject.ic === this.ic) {
        this.showMsg('刷卡操作成功', 'green')
      }
    }
  },
}
</script>

<style scoped>
.app-box {
  padding: 0px;
}
</style>
